package com.es.hr.shop;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.es.business.area.AreaBean;
import com.es.business.area.AreaBiz;
import com.es.business.city.CityBean;
import com.es.business.city.CityBiz;
import com.es.business.citys.CitysBean;
import com.es.business.citys.CitysBiz;
import com.es.business.province.ProvinceBean;
import com.es.business.province.ProvinceBiz;
import com.es.hr.emp.EmpInfoBean;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class ShopInsertDoAction extends ActionSupport
implements ServletRequestAware{
	 HttpServletRequest request;

	public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
		// TODO Auto-generated method stub
		this.request = paramHttpServletRequest;
	}
	
	public String execute() throws Exception{
		String result = "";
		ShopBiz shopBiz = new ShopBiz();
		
		ShopBean shopBean= this.getShopBBean(request);
		
		String areaCode = this.request.getParameter("emp_area_code");
		areaCode = areaCode==null?"":areaCode;
		AreaBean areaBean = new AreaBiz().getAreaByCode(areaCode);
		shopBean.setShop_area_code(areaCode);
		shopBean.setShop_area_value(areaBean.getAreaValue());
		
		String provinceCode = this.request.getParameter("emp_province_code");
		provinceCode = provinceCode==null?"":provinceCode;
		ProvinceBean provinceBean = new ProvinceBiz().getProvinceByCode(provinceCode);
		shopBean.setShop_province_code(provinceCode);
		shopBean.setShop_province_value(provinceBean.getProvinceName());
		
		String cityCode = this.request.getParameter("emp_city_code");
		cityCode = cityCode==null?"":cityCode;
		CityBean cityBean = new CityBiz().getCityByCode(cityCode);
		shopBean.setShop_city_code(cityCode);
		shopBean.setShop_city_value(cityBean.getCityName());
		
		String citysCode = cityBean.getCitysCode();
		CitysBean citysBean = new CitysBiz().getCitysByCode(citysCode);
		shopBean.setShop_citys_code(citysCode);
		shopBean.setShop_citys_value(citysBean.getCitysValue());
		
		shopBean.setShop_ctm_code(this.request.getParameter("emp_ctm_id"));
		shopBean.setShop_ctm_name(this.request.getParameter("emp_ctm_name"));
		
		boolean res = shopBiz.insertShop(shopBean);
		if(res == true){
			result = "success";
			String message = "添加成功";
			this.request.setAttribute("message", message);
		}else{
			result = "success";
			String message = "添加失败，请重新添加";
			this.request.setAttribute("message", message);
		}
		return result;
	}
	
	public ShopBean getShopBBean(HttpServletRequest request) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException{
		Field[] fds = Class.forName("com.es.hr.shop.ShopBean").getDeclaredFields();   
		ShopBean bean = new ShopBean();
		 
		  for(int i=0;i<fds.length;i++)   
		  {   
			  String d= fds[i].getName();
			  fds[i].set(bean, request.getParameter(d));
		  }
		  return bean;
	}
	 

}
